import java.io.*;
import java.sql.*;



/* Version 0.0.1
   Author: HussuLinux: hussulinux@gmail.com
   Please send updated class files so that we can keep in sync.... thanks... 
   Dated 25th October, 2008
   Its a very dirty code, just done in 1 hour for something, but the concept is good, so please it needs to be more generic, i will do it when i get time....
*/
public class Crudder {
	public static void main(String[] args) {
		String dbType=null;
		String dbName=null;
		String hostName=null;
		String userName=null;
		String password=null;
		String tableName=null;
		String descTblQry = null;
		
		try{
		dbType = args[0];
		dbName = args[1];
		hostName = args[2];
		userName = args[3];
		password = args[4];
		tableName = args[5];
		}
		catch ( Exception E){
			System.out.println("Usage:");
			System.out.println("------------------------------------------------------------------------");
			System.out.println("Crudder <dbType> <dbName> <hostName> <userName> <password> <tableName>");
			System.out.println("dbType = Your database type e.g oracle or mysql");
			System.out.println("dbName = Your database Name or Service Name here e.g XE or test");
			System.out.println("userName = Your Username or Schema Name e.g HR, SCOTT or root");
			System.out.println("password = Your Password e.g TIGER, AXYS2008 ");
			System.out.println("tableName = Your table type e.g EMP or TEST");
			//return;
			
		}
		
		String [] fields = new String[150];
		//--------DEFAULT SETTINGS-------------
		if (dbType==null )	dbType = "mysql";
		if (dbName==null )	dbName = "test";  //Database Name or Service Name
		if (hostName==null) hostName = "localhost";
		if (userName==null) userName = "root";
		if (password==null) password = "";
		if (tableName==null) tableName = "nazia";
		//--------------------------------------
		
		String driverName="";
		String connStr = "";
		if (dbType.equalsIgnoreCase("mysql")){
			driverName = "com.mysql.jdbc.Driver";
			connStr = "jdbc:mysql://" + hostName + "/" + dbName ;
			descTblQry = "desc " + tableName;
		}
		else if (dbType.equalsIgnoreCase("oracle")){
			driverName = "oracle.jdbc.driver.OracleDriver";
			connStr = "jdbc:oracle:thin:@" + hostName + ":1523:" + dbName ;
			descTblQry = "select column_name from all_tab_columns where lower(table_name) =  '" + tableName.toLowerCase() + "'";
		}
		else{
			System.out.println("The database is NOT supported");
			return;
		}
		try{
		Class.forName (driverName).newInstance ();
		Connection conn = DriverManager.getConnection (connStr, userName, password);
		System.out.println(descTblQry);
		ResultSet rs = conn.createStatement().executeQuery(descTblQry);
		
		int c=0;
		while(rs.next()){
			fields[c] = rs.getString(1);
			c++;
		}
		
		
		FileOutputStream out = new FileOutputStream("c:\\" + tableName + ".java");
		
		PrintStream p = new PrintStream( out );

		p.println ("public class " + tableName);
		p.println ("{ ");					//<-- Open class braces
		p.println ("String sql; ");
		
		//------------- Generating CONSTRUCTOR-----------------------
		p.println("public "+ tableName + "() {" );
		p.println("   Class.forName (\""+ driverName+"\").newInstance ();");
		p.println ("} " );
		
		//---------------GENERATING INSERT METHOD---------------
		p.print ("public int insert( " );
		
		for (int i=0;i<fields.length;i++){
				if (fields[i]!=null){
					p.print ("String " + fields[i] );
					if (fields[i+1]!=null){
						p.print ("," );
					}
				}
		}

		p.println (") {" );
		
		p.println(" try { ");
		p.println("   Connection conn = DriverManager.getConnection (\""+ connStr +"\", \"" + userName + "\" , \"" + password + "\" );");
		p.print ("   sql = \"insert into " + tableName + " values ( " );
		
		for (int i=0;i<fields.length;i++){
			if (fields[i]!=null){
				p.print ("'\" + " + fields[i] + " + \"'" );
				if (fields[i+1]!=null){
					p.print ("," );
				}
			}
		}
		
		p.println(" ) \"); ");
		p.println("   int retVal = conn.createStatement().executeUpdate(sql);");
		p.println("   conn.close();");
		p.println("   return retVal;");
		
		p.println("   }");
		p.println(" catch(Exception e) {");
		p.println("		e.printStackTrace(); return 0;");
		p.println("	} ");
		
		
		p.println("}");  //<-- closing method Braces
		
		
		//---------------------------------------------------------
		
		
		
		// ------------------GENERATING DELETE METHOD----------------
		p.print ("public int delete( " );
		
		for (int i=0;i<fields.length;i++){
				if (fields[i]!=null){
					p.print ("String " + fields[i] );
					if (fields[i+1]!=null){
						p.print ("," );
					}
				}
		}

		p.println (") {" );
		
		p.println(" try { ");
		p.println("   Connection conn = DriverManager.getConnection (\""+ connStr +"\", \"" + userName + "\" , \"" + password + "\" );");
		p.print ("   sql = \"delete from " + tableName + " where  " );
		
		for (int i=0;i<fields.length;i++){
			if (fields[i]!=null){
				p.print (fields[i] + "='\" + " + fields[i] + " + \"'" );
				if (fields[i+1]!=null){
					p.print (" and " );
				}
			}
		}
		
		p.println(" ) \"); ");
		p.println("   int retVal = conn.createStatement().executeUpdate(sql);");
		p.println("   conn.close();");
		p.println("   return retVal;");
		
		
		p.println("   }");
		p.println(" catch(Exception e) {");
		p.println("		e.printStackTrace();  return 0;");
		p.println("	}");
		
		p.println("}");//<-- closing method Braces
		
		
		//-----------------------------------------------------------------------
		
		
		p.println("}");//<-- closing Classes Braces
		p.close();

		System.out.println("Generated File at : c:\\" + tableName + ".java");
		
		rs.close();
		conn.close();
		
		}
		
		catch(Exception e){
			e.printStackTrace();
			
		}
		
	}
}
